home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / asmbler.arc / PATSEARC.ASM < prev    next >
Assembly Source File  |  1988-11-19  |  6KB  |  192 lines

  1. ; [03-Feb-84]
  2. ; Replaced case-sensitive search in PATSEARCH by with one to ignore case
  3. ; or optionally, require exact case, in matching.
  4.  
  5. IGNORECASE EQU 1                ; 0 for case-sensitive search
  6.  
  7. assume  cs:utilities
  8.  
  9. public  patsearch
  10.  
  11. utilities segment para public 'code'
  12.  
  13. enter   macro
  14. ; Enter a routine and set up bp to point to the argument frame on the stack
  15.         push    bp
  16.         mov     bp,sp
  17.         add     bp,6
  18.         endm
  19.  
  20. exit    macro
  21. ; Restore stack before "ret n" in a routine entered by the "enter" macro
  22.         pop     bp
  23.         endm
  24.  
  25. assume  cs:utilities
  26.         db      "<<xnzscasb>>"
  27. xnzscasb proc   near
  28. ;xnzscasb:
  29. ;
  30. ; Begin translated equivalent of REPNZ   SCASB
  31. ;
  32.         push    ax              ; save ax and
  33.         push    bx              ; bx during this loop
  34.         mov     bx, offset cs:xtable
  35.         xlat    byte ptr cs:xtable ; al <- xtable(al) - translated pattern byte
  36.         mov     ah,al           ; translated pattern byte
  37.  
  38. loop1:
  39.         mov     al,es:byte ptr [di]     ; source byte
  40.         xlat    byte ptr cs:xtable ; translate source byte in al
  41.         inc     di              ; point to next byte (like scasb does)
  42.         cmp     ah,al           ; source = pattern?
  43.         loopne  loop1           ; decrement cx and loop while no match
  44.  
  45.         pop     bx              ; restore bx
  46.         pop     ax              ; and ax
  47.         ret
  48. ;
  49. ; End translated equivalent of REPNZ   SCASB
  50. ;
  51.  
  52. xtable  equ     $       ; translation table mapping lower case into upper
  53. xbyte=0
  54.         rept    97      ; NUL .. "`" --> themselves
  55.         db      xbyte
  56. xbyte=xbyte+1
  57.         endm
  58. xbyte=65
  59.         rept    26      ; "a".."z" --> "A".."Z"
  60.         db      xbyte
  61. xbyte=xbyte+1
  62.         endm
  63. xbyte=123
  64.         rept    133     ; "["..0FFH --> themselves
  65.         db      xbyte
  66. xbyte=xbyte+1
  67.         endm
  68.  
  69.  
  70. xnzscasb endp
  71.  
  72. assume  cs:utilities
  73.         db      "<<xzcmpsb>>"
  74. xzcmpsb proc    near
  75. ;xzcmpsb:
  76. ;
  77. ; Begin translated equivalent of REPZ CMPSB
  78. ;
  79.         push    ax              ; save ax and
  80.         push    bx              ; bx during this loop
  81.         mov     bx, offset cs:xtable
  82.  
  83. loop2:
  84.         mov     al,ds:byte ptr [si];pattern byte
  85.         xlat    byte ptr cs:xtable ; al <- xtable(al) - translated pattern byte
  86.         mov     ah,al           ; translated pattern byte
  87.         mov     al,es:byte ptr [di]     ; source byte
  88.         xlat    byte ptr cs:xtable ; translate source byte in al
  89.         inc     si              ; point to next byte (like cmpsb does)
  90.         inc     di              ; point to next byte (like cmpsb does)
  91.         cmp     ah,al           ; source = pattern?
  92.         loope   loop2           ; decrement cx and loop while match
  93.  
  94.         pop     bx              ; restore bx
  95.         pop     ax              ; and ax
  96.         ret
  97.  
  98. ;
  99. ; End translated equivalent of REPZ CMPSB
  100. ;
  101. xzcmpsb endp
  102.  
  103.         db      "<<patsearch>>"
  104. patsearch proc  far             ; string search
  105.  
  106. ; function patsearch(    exactmatch: boolean;
  107. ;                        const pattern;
  108. ;                        searchbuffer: adsmem;
  109. ;                        maxcount: word;
  110. ;                        ): word;
  111. ;
  112. ; After "enter", stack looks like
  113. ;
  114. ;   0 - maxcount
  115. ;   2 - address of searchbuffer
  116. ;   4 - segment of searchbuffer
  117. ;   6 - address of pattern
  118. ;   8 - maximum length of pattern (0..255)
  119. ;  10 - exactmatch (0 = FALSE, 1 = TRUE)
  120.  
  121.         enter
  122.         mov     cx,[bp]         ; grab the maximum count
  123.         test    cx,cx
  124.         jz      pdone
  125.         mov     di,[bp+2]       ; grab the address of the search buffer
  126.         mov     es,[bp+4]       ; grab the segment of the search buffer
  127.         mov     si,[bp+6]       ; grab the address of the pattern
  128.         mov     bx,[bp+10]      ; grab the exactmatch flag
  129.         cld                     ; set direction for forward search
  130.         lodsb                   ; grab the length of the pattern in al
  131.         test    al,al           ; are there any characters in the pattern?
  132.         jz      pdone           ; no
  133.  
  134.         mov     ah,al           ; save length of pattern in al
  135.         dec     ah              ; one less chars than length(pattern)
  136.         lodsb                   ; put the first char of pattern in al
  137.  
  138. ; set up for scan
  139. ; ax contains the first char of the pattern
  140. ; bx contains the exactmatch flag
  141. ; cx contains count of maximum chars to scan for
  142. ; di contains the starting address for search in sbuffer
  143. ; si contains the address of the rest of the string
  144.  
  145. doscan:
  146.         test    bx,bx           ; exactmatch = 0 (FALSE) or 1 (TRUE)?
  147.         jnz     exactscan       ; TRUE
  148.         call    xnzscasb        ; find the first character, ignoring case
  149.         jmp     short scandone
  150.  
  151. exactscan:
  152.         repne   scasb           ; find the first character, matching case 
  153.  
  154. scandone:
  155.         jne     pdone           ; character not found so we are done
  156.         push    cx              ; save remaining count
  157.         push    di              ; save buffer pointer for next scan
  158.         push    si              ; save pattern pointer
  159.         test    ah,ah
  160.         jz      match
  161.         mov     cl,ah
  162.         xor     ch,ch
  163.         test    bx,bx           ; exactmatch = 0 (FALSE) or 1 (TRUE)?
  164.         jnz     exactcompare    ; TRUE
  165.         call    xzcmpsb         ; compare strings ignoring case
  166.         jmp     short comparedone
  167.  
  168. exactcompare:
  169.         repe    cmpsb           ; compare strings for exact case match
  170.  
  171. comparedone:
  172.         je      match
  173.         pop     si
  174.         pop     di
  175.         pop     cx
  176.         jmp     doscan
  177.  
  178. match:
  179.         add     sp,4            ; pop off si and di
  180.         pop     cx
  181.         inc     cx
  182.  
  183. pdone:
  184.         mov     ax,[bp]         ; chars scanned to find pattern
  185.         sub     ax,cx
  186.         exit
  187.         ret     12              ; return discarding arguments
  188. patsearch endp        
  189.  
  190. utilities ends
  191.         end
  192.